//
//  ZSelectColorVc.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/19.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZSelectColorVc.h"
#import "ZZSelectColorCell.h"
#import "ZZCollectColorTool.h"

#define kZZSelectColorCellIdentifier @"ZZSelectColorCellIdentifier"
@interface ZSelectColorVc ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *customColorLabel;

@property (weak, nonatomic) IBOutlet UILabel *clickColorLabel;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, strong)NSMutableArray *colors;

@property (nonatomic, assign)CGPoint originCenter;

@end

@implementation ZSelectColorVc

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *label = [UILabel new];
    label.font = [UIFont systemFontOfSize:17];
    label.text = NSLocalizedString(@"HOME_Collect_VCtitle", nil);
    label.textColor = [UIColor whiteColor];
    label.frame = CGRectMake(0, 0, 100, 44);
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"icon_fanh"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 32, 32);
    [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchDown];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZZSelectColorCell" bundle:nil] forCellWithReuseIdentifier: kZZSelectColorCellIdentifier];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}


#pragma mark - collectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.colors.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZZSelectColorCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:kZZSelectColorCellIdentifier forIndexPath:indexPath];
    cell.colorViewColor = self.colors[indexPath.row];
    UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(cellMove:)];
    [cell addGestureRecognizer:pan1];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    UIColor *color = self.colors[indexPath.row];

    NSString *str = [[[ZZCollectColorTool new] group] reacts][indexPath.row];
    CGRect react = CGRectFromString(str);

    if (self.outPutColor) {

        self.outPutColor(color,react);
    }
}

- (void)cellMove:(UIPanGestureRecognizer *)pan {
    
    CGPoint originCenter = pan.view.center;
    CGPoint  point = [pan translationInView:self.collectionView];
    CGFloat width = self.collectionView.width - 62.0 / 2;
    CGFloat height = self.collectionView.height - 62.0 / 2;
    CGRect originalFrame = pan.view.frame;
    
    originalFrame.origin.x += point.x;
    originalFrame.origin.y += point.y;
    
    // 圆心
    CGPoint center = CGPointMake(CGRectGetMidX(originalFrame) - width / 2,CGRectGetMidY(originalFrame) - width /2);
    
    CGFloat offetX  = 62.0 / 2;
    CGFloat offerY = 62.0 / 2;
 
    CGFloat x2 = fabs(center.x - offetX ) * fabs(center.x - offetX);
    CGFloat y2 = fabs(center.y - offerY)  * fabs(center.y  - offerY) ;
    
   
    pan.view.frame = originalFrame;
    [pan setTranslation:CGPointZero inView:pan.view];
    
    switch (pan.state) {
            
        case UIGestureRecognizerStateBegan: {
            
            self.originCenter = pan.view.center;
        }
            break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateFailed:
        case UIGestureRecognizerStateCancelled:
        {
            
            CGFloat x1 = self.originCenter.x;
            CGFloat x2 = originCenter.x;
            CGFloat y1 = self.originCenter.y;
            CGFloat y2 = originCenter.y;
            CGFloat offer = sqrt((x1 - x2)* (x1 - x2) + (y1 - y2)*(y1 - y2));
            
            if ( offer > 62.0) {
                
                NSInteger  index = [self.collectionView indexPathForCell:pan.view].row;
                [self.colors removeObjectAtIndex:index];
                [[[ZZCollectColorTool new] group].reacts removeObjectAtIndex:index];
                 [[ZZCollectColorTool new]save];
            };
             [self.collectionView reloadData];
           
            // 输出当前ImageView上的颜色值
//            UIColor*color = [self colorOfPoint:pan.view.center];
//            if (self.viewDidClick) {
//
//                self.viewDidClick(color,A);
//            }
            
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - Func
- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSMutableArray *)colors {
    
    if (!_colors) {
        
         _colors = [[ZZCollectColorTool new] group].colors;
        }
    return _colors;
}


@end
